package fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.jianke.R;
import com.jianke.R2;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.app.EventBusTagUtils;
import com.lib.bean.Event;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import bean.ChooseDataBean;
import bean.ClassListBean;
import butterknife.BindView;

public class JingTiaoClassFragment extends BaseAppFragment {
    @BindView(R2.id.recy_list)
    RecyclerView recyList;
    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    List<ClassListBean.ObjBean.RowsBean> classList = new ArrayList<>();

    ClassListAdapter classListAdapter;

    private boolean visibleToUSer = false;
    private int page = 0;

    private String subject = "";
    private String grade = "";

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initJingTiaoData();
            }
        });

        initView();

        initJingTiaoData();

    }

    private void initView() {

        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setEnableFooter(true);

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                //上拉加载时，清空筛选数据
                subject = "";
                grade = "";

                page = 0;
                initJingTiaoData();
                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++page;
                initJingTiaoData();
                springView.onFinishFreshAndLoad();
            }
        });

        classListAdapter = new ClassListAdapter(getActivity(), classList);
        recyList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        recyList.setNestedScrollingEnabled(false);
        recyList.setAdapter(classListAdapter);

    }

    private void initJingTiaoData() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "TIPCLASS_RECOMMEND_SCREEN")
                .addParam("type", "2")
                .addParam("page", page)
                .addParam("limit", 10)
                .addParam("subject_id", subject)
                .addParam("grade_id", grade)
                .execute(new HttpNormalCallBack<ClassListBean>() {
                    @Override
                    public void onSuccess(ClassListBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (page == 0)
                                classList.clear();

                            setListData(result.getObj().getRows());

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }

                    @Override
                    public void onError(String e) {

                        if (stateView != null) {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }
                });


    }


    private void setListData(List<ClassListBean.ObjBean.RowsBean> obj) {

        classList.addAll(obj);
        classListAdapter.updateData(classList);
        classListAdapter.notifyDataSetChanged();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list;
    }


    //-------------------------------------------------------------------------------------精挑课推荐
    class ClassListAdapter extends BaseAdapter<ClassListBean.ObjBean.RowsBean> {

        public ClassListAdapter(Context context, List<ClassListBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_jingtiao_1;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<ClassListBean.ObjBean.RowsBean> mData) {

            ImageView ishowImg = holder.getView(R.id.iv_jingtiao_img);
            Glide.with(getActivity())
                    .load(mData.get(position).getCover())
                    .apply(GlideConfig.getRoundOptions1(20))
                    .into(ishowImg);

            holder.setText(R.id.tv_jingtiao_title, mData.get(position).getTitle());
            holder.setText(R.id.tv_jingtiao_classNum, "共" + mData.get(position).getVideo_num() + "节");

            holder.getView(R.id.lin_jingtiao_item).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");
                    ARouter.getInstance().build(ARouterPathUtils.Tablet_Video_RecordDetailActivity)
                            .withString("tcid", mData.get(position).getTcid() + "").navigation();
                }
            });
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            visibleToUSer = true;
        } else {
            visibleToUSer = false;
        }

    }

    @Subscriber(tag = EventBusTagUtils.SureChooseRefresh)
    public void fromSureChooseRefresh(Event event) {

        switch (event.getEventCode()) {
            case 1:
                if (visibleToUSer) {

                    LogUtil.e("在JingTiaoClass中筛选了数据");
                    ChooseDataBean dataBean = (ChooseDataBean) event.getData();
                    if ("".equals(dataBean.getGrade()) || null == dataBean.getGrade()) {
                        LogUtil.e("年级为空");
                        subject = dataBean.getCourse();
                    } else if ("".equals(dataBean.getCourse()) || null == dataBean.getCourse()) {
                        LogUtil.e("科目为空");
                        grade = dataBean.getGrade();
                    } else {
                        subject = dataBean.getCourse();
                        grade = dataBean.getGrade();
                        LogUtil.e("用户筛选数据科目的Id：" + subject + "，年级的Id为：" + grade);
                    }

                    initJingTiaoData();
                }

                break;
        }
    }
}
